﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PushBox.Core;
using PushBox.WinApp.Properties;

namespace PushBox.WinApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.SetStyle(
                ControlStyles.OptimizedDoubleBuffer
                | ControlStyles.ResizeRedraw
                | ControlStyles.Selectable
                | ControlStyles.AllPaintingInWmPaint // 禁止擦除背景.
                | ControlStyles.UserPaint
                | ControlStyles.SupportsTransparentBackColor
                | ControlStyles.DoubleBuffer  // 双缓冲 双缓冲解决窗体闪
                , true);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string mapDraw = @"
 ########
##  +  +#
#   *#  #
#  * #  ##
#  @     #
##########
";
            mapInfos.Enqueue(mapDraw);
            mapDraw = @"
 ###########
##  +     +#
#   *#     #
#  * #    ##
#        ##
#  @     #
#        #
##########
";
            mapInfos.Enqueue(mapDraw);
            mapDraw = @"
 ##########
##  +     #
#   *#    #
#  @     #
#        #
##########
";
            mapInfos.Enqueue(mapDraw);


            Next();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {

            switch (e.KeyCode)
            {
                case Keys.Up:
                    map.Gamer.TryMove(MoveType.Up);
                    break;
                case Keys.Down:
                    map.Gamer.TryMove(MoveType.Down);
                    break;
                case Keys.Left:
                    map.Gamer.TryMove(MoveType.Left);
                    break;
                case Keys.Right:
                    map.Gamer.TryMove(MoveType.Right);
                    break;
            }
        }

        static Map map;

        static Queue<string> mapInfos = new Queue<string>();


        void Next()
        {
            if (mapInfos.Count == 0) { MessageBox.Show("没地图了!"); return; }
            string m = mapInfos.Dequeue();
            map = new Map(m, ShowMap);
            map.Passed += Map_Passed;
        }

        private void Map_Passed(Map sender)
        {
            MessageBox.Show("通关了");
            Next();
        }

        void ShowMap(Map m)
        {
            this.Controls.Clear();
            for (int y = 0; y < m.Height; y++)
            {
                for (int x = 0; x < m.Width; x++)
                {
                    Position p = m.GetObject(x, y);
                    if (p == null) { continue; }
                    PictureBox pb = new PictureBox();
                    pb.Size = new Size(32, 32);
                    pb.Location = new Point(p.X * 32, p.Y * 32);
                    if (p is Wall)
                    {
                        pb.Image = Resources.wall;
                    }
                    else if (p is Player)
                    {
                        pb.Image = Resources.p_d;
                    }
                    else if (p is Target)
                    {
                        pb.Image = Resources.key;
                    }
                    else if (p is Box)
                    {
                        pb.Image = Resources.box;
                    }
                    this.Controls.Add(pb);
                }
            }
        }
    }
}



